package com.example.controller;

import com.example.dao.RoleMapper;
import com.example.dao.URrelationMapper;
import com.example.dao.UserMapper;
import com.example.entity.OP;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import com.example.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.IOException;


@CrossOrigin
@RestController
public class HomeIndexController {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private URrelationMapper uRrelationMapper;
    @Autowired
    private RoleMapper roleMapper;
    @RequestMapping(value = "/login", method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String login(@RequestBody User user) {
        String username = user.getName();
        String password = user.getPsd();
        String realname = userMapper.selectByName(username).getRealname();
        // 从SecurityUtils里边创建一个 subject
        Subject subject = SecurityUtils.getSubject();
        // 在认证提交前准备 token（令牌）
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        // 执行认证登陆
        try {
            subject.login(token);
        } catch (UnknownAccountException uae) {
            return "未知账户";
        } catch (IncorrectCredentialsException ice) {
            return "密码不正确";
        } catch (LockedAccountException lae) {
            return "账户已锁定";
        } catch (ExcessiveAttemptsException eae) {
            return "用户名或密码错误次数过多";
        } catch (AuthenticationException ae) {
            return "用户名或密码不正确！";
        }
        if (subject.isAuthenticated()) {
            return "登录成功&"+realname;
        } else {
            token.clear();
            return "登录失败";
        }
    }

    @RequestMapping(value = "/getStatus", method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
    public String getStatus() {
        Subject subject = SecurityUtils.getSubject();
        String status = String.valueOf(subject.isAuthenticated());
        return status;
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
    public String logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "Log out success!";
    }

    @RequestMapping(value = "/notRole",method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
    public String notRole() {

        return "notRole";
    }

    @RequestMapping(value = "/getOpInfo",method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
    public String getOpInfo() throws IOException{
        OP op = new OP();
        Subject subject = SecurityUtils.getSubject();
        String username = subject.getPrincipal().toString();
        int userid = userMapper.selectByName(username).getId();
        Integer roleID = uRrelationMapper.selectRoleIDByUserID(userid);
        String role = roleMapper.selectByPrimaryKey(roleID).getRoleName();
        op.setName(username);
        op.setRole(role);
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(op);
    }



}
